package hdgeom.primitives;

import java.util.ArrayList;

import processing.core.PVector;

public class PolyLine {
public ArrayList<PVector>vertices=new ArrayList<PVector>();
public PVector getPoint(int i){
	return vertices.get(i);
}
public PVector getTangent(int i){
	PVector p1=null;
	PVector p2=null;
	if (i==vertices.size()-1){
		 p1=vertices.get(i-1);
		 p2=vertices.get(i);
	}
	 p1=vertices.get(i);
	 p2=vertices.get(i+1);
	 PVector p12=PVector.sub(p2, p1);
	 p12.normalize();
	 return p12;
	
}
public PVector getPointRelative(float t){
	float sum=0;
	float l=getLength();
	float stop=t*l;
	for (int i=0;i<vertices.size()-1;i++){
		PVector p1=vertices.get(i);
		PVector p2=vertices.get(i+1);
		PVector p12=PVector.sub(p1, p2);
		float cL=p1.dist(p2);
		if ((sum+cL)>=stop){
			float rL=cL-sum;
			p12.normalize();
			p12.mult(rL);
			p12.add(p1);
			return p12;
		}
	}
	return null;
}
public PVector getTangentRelative(float t){
	float sum=0;
	float l=getLength();
	float stop=t*l;
	for (int i=0;i<vertices.size()-1;i++){
		PVector p1=vertices.get(i);
		PVector p2=vertices.get(i+1);
		PVector p12=PVector.sub(p1, p2);
		float cL=p1.dist(p2);
		if ((sum+cL)>=stop){
			p12.normalize();
			return p12;
		}
	}
	return null;
}
public float getLength(){
	float sum=0;
	for (int i=0;i<vertices.size()-1;i++){
		sum+=vertices.get(i).dist(vertices.get(i+1));
	}
	return sum;
}
}

